package com.sudao.android.ball;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sudao.android.ball.GameView.GameThread;

public class GameActivity extends Activity {

    private static final int MENU_NEW =  1;
    //private static final int MENU_PAUSE =  2;
    private static final int MENU_RESUME = 3;
    private static final int MENU_BEST = 4;
    private static final int MENU_EXIT = 5;
	private static final int MENU_SOUND_CONTROL = 6;
	private static final int MENU_HELP = 7;

	private static final int REQUEST_HELP = 1;
	private static final int REQUEST_SOUND_CONTROL = 2;
	private static final int REQUEST_BEST = 3;

    private GameThread mGameThread;

    private GameView mGameView;

	private static MusicService mBoundService;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((MusicService.LocalBinder)service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };
	
	public static MusicService getSoundService() {
		return mBoundService;
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
		
        menu.add(0, MENU_NEW, 0, R.string.menu_new).setIcon(R.drawable.ic_menu_new);
        //menu.add(0, MENU_PAUSE, 0, R.string.menu_pause).setIcon(R.drawable.ic_menu_pause);
        menu.add(0, MENU_RESUME, 0, R.string.menu_resume).setIcon(R.drawable.ic_menu_resume);
        menu.add(0, MENU_BEST, 0, R.string.menu_best).setIcon(R.drawable.ic_menu_best);
        menu.add(0, MENU_EXIT, 0, R.string.menu_exit).setIcon(R.drawable.ic_menu_exit);  
        menu.add(0, MENU_SOUND_CONTROL, 0, R.string.menu_sound_control).setIcon(R.drawable.ic_menu_sound_control);  
        menu.add(0, MENU_HELP, 0, R.string.menu_help).setIcon(R.drawable.ic_menu_help);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_NEW:
                mGameThread.startGame();
                return true;
            case MENU_EXIT:
				finish();
                return true;
            /*case MENU_PAUSE:
                mGameThread.pause();
                return true;*/
            case MENU_RESUME:
                mGameThread.unpause();
                return true;
            case MENU_HELP:
                mGameThread.pause();
	            Util.openLocalPage(this, "file:///android_asset/html/menu_help.html", REQUEST_HELP);
                return true;
            case MENU_SOUND_CONTROL:
                mGameThread.pause();
				startActivityForResult(new Intent(this, SoundControl.class), REQUEST_SOUND_CONTROL);
                return true;
            case MENU_BEST:
                mGameThread.pause();
				startActivity(new Intent(this, BestRecordActivity.class));
                return true;
        }
        return false;
    }

	public void setGameThread(GameThread thread) {
		mGameThread = thread;
	}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

		final Window win = getWindow();
		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	
		bindService(new Intent(GameActivity.this, MusicService.class), mConnection, Context.BIND_AUTO_CREATE);

        setContentView(R.layout.game_layout);

        mGameView = (GameView) findViewById(R.id.game);
        mGameThread = mGameView.getThread();

		mGameView.setGameActivity(this);
        mGameView.setImageView((ImageView) findViewById(R.id.text));
        LinearLayout boardView = (LinearLayout) findViewById(R.id.board);
        mGameView.setBoardHeight(boardView.getHeight());
        mGameView.setScoreTextView((TextView) boardView.findViewById(R.id.score));
        mGameView.setLifeTextView((TextView) boardView.findViewById(R.id.life));
        mGameView.setLevelTextView((TextView) boardView.findViewById(R.id.level));

		/*
        LinearLayout control = (LinearLayout) findViewById(R.id.control);
		Button btn[] = new Button[3];
		btn[0] = (Button) control.findViewById(R.id.button1);
		btn[1] = (Button) control.findViewById(R.id.button2);
		btn[2] = (Button) control.findViewById(R.id.button3);
		mGameView.setControlButton(btn);
		*/

        if (savedInstanceState == null) {
            mGameThread.setState(GameView.STATE_FIRST_START);
		    loadPreferences();
        } else {
            mGameThread.restoreState(savedInstanceState);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGameView.getThread().pause(); // pause game when Activity pauses
    }

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		unbindService(mConnection);
		super.onDestroy();
	}

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mGameThread.saveState(outState);
    }

	@Override
	protected void onRestoreInstanceState(Bundle inState) {
		super.onRestoreInstanceState(inState);
	}

	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && data != null) {
			switch (requestCode) {
        	case REQUEST_HELP:
            	break;
			case REQUEST_SOUND_CONTROL:
				int padSpeed = data.getExtras().getInt(SoundControl.PREF_PAD_SPEED);
				boolean sound = data.getExtras().getBoolean(SoundControl.PREF_SOUND);
				float soundVolume = data.getExtras().getFloat(SoundControl.PREF_SOUND_VOLUME);

				mGameView.setPadSpeed(padSpeed);
				GameActivity.getSoundService().setPlaySound(sound);
				GameActivity.getSoundService().setPlaySoundVolume(soundVolume);
				break;
			}
		}
	}

	private void loadPreferences() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

		boolean sound = pref.getBoolean(SoundControl.PREF_SOUND, SoundControl.DEFAULT_PREF_SOUND);
		float soundVolume = pref.getInt(SoundControl.PREF_SOUND_VOLUME, SoundControl.DEFAULT_PREF_SOUND_VOLUME) / 100f;
		int padSpeed = Integer.parseInt(pref.getString(SoundControl.PREF_PAD_SPEED, 
			SoundControl.DEFAULT_PREF_PAD_SPEED + ""));
		
		mGameView.setPadSpeed(padSpeed);
	}
}
